package com.cmc.cloud.cmclink.doc.controller.admin.special;

import com.cmc.cloud.cmclink.doc.constants.UrlConstants;
import com.cmc.cloud.cmclink.doc.service.SpecialCargoApplyService;
import com.cmc.cloud.cmclink.doc.service.SpecialCargoApprovalService;
import com.cmc.cloud.cmclink.doc.vo.specialcargoapplyvo.*;
import com.cmes.framework.biz.advice.annotations.UseResponsePropertyAdvice;
import com.cmes.framework.common.pojo.CommonResult;
import com.cmes.framework.common.pojo.PageResult;
import com.cmes.framework.excel.core.util.ExcelUtils;
import com.cmes.framework.operatelog.core.annotations.OperateLog;
import com.cmes.framework.operatelog.core.enums.OperateTypeEnum;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;


/**
 * 特种货审批主信息前端接口Controller
 *
 * @author codegen
 */
@Tag(name = "管理后台 - 特种货审批主信息")
@RestController
@Validated
@RequestMapping(UrlConstants.APPLICATION_URL + "/specialCargoApproval")
public class SpecialCargoApprovalController {

    @Resource
    private SpecialCargoApplyService specialCargoApplyService;
    @Resource
    private SpecialCargoApprovalService specialCargoApprovalService;

    @Operation(summary = "获得特种货审批主信息分页")
    @PostMapping("/page")
    @PreAuthorize("@ss.hasPermission({'doc:specialApprove:query','doc:specialApproveInland:query'})")
    @OperateLog(name = "获得特种货审批主信息分页", type = OperateTypeEnum.GET)
    @UseResponsePropertyAdvice
    public CommonResult<PageResult<ApplyAndVoyageInfoVO>> getSpecialCargoApplyPage(@Valid @RequestBody SpecialCargoApplyPageReqVO pageVo) {
        PageResult<ApplyAndVoyageInfoVO> results = specialCargoApplyService.getSpecialCargoApplyPage(pageVo);
        return CommonResult.success(results);
    }

    @GetMapping("/export")
    @PreAuthorize("@ss.hasPermission({'doc:specialApprove:export','doc:specialApproveInland:export'})")
    @Operation(summary = "excel导出")
    @OperateLog(name = "excel导出", type = OperateTypeEnum.EXPORT)
    @UseResponsePropertyAdvice
    public void exportExcelTemplate(HttpServletResponse response, SpecialCargoApplyPageReqVO reqVo) throws IOException {
        List<ApplyAndVoyageInfoExportVO> exportList = specialCargoApplyService.getExportList(reqVo);
        response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("SpecialCargoApply编码导入模板.xlsx", StandardCharsets.UTF_8.name()));
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
        ExcelUtils.write(response, "SpecialCargoApply.xls", "模版", ApplyAndVoyageInfoExportVO.class, exportList);
    }

    @Operation(summary = "获得特种货审批详细信息")
    @GetMapping("/get/{id}")
    @PreAuthorize("@ss.hasPermission({'doc:specialApprove:preview','doc:specialApproveInland:preview'})")
    @Parameter(name = "id", description = "获取审批详情", required = true, example = "1024")
    @OperateLog(name = "获得特种货审批详细信息", type = OperateTypeEnum.GET)
    public CommonResult<ApprovalDetailRespVO> getApprovalDetailInfo(@PathVariable("id") Long id) {
        return CommonResult.success(specialCargoApprovalService.getApprovalDetailInfo(id));
    }

    @Operation(summary = "获得showReply信息")
    @GetMapping("/showReplay/{id}")
    @Parameter(name = "id", description = "showReplay按钮", required = true, example = "1024")
    @OperateLog(name = "获得showReply信息", type = OperateTypeEnum.GET)
    public CommonResult<String> showReplay(@PathVariable("id") Long id) {
        return CommonResult.success(specialCargoApprovalService.showReplay(id));
    }

    @Operation(summary = "更新特种货审批状态和描述")
    @PostMapping("/updateApprovalStatus")
    @OperateLog(name = "更新特种货审批状态和描述", type = OperateTypeEnum.UPDATE)
    public CommonResult<Void> updateApprovalStatus(@Valid @RequestBody VoyageApprovalUpdateReqVO updateReqVo) {
        specialCargoApprovalService.updateApprovalStatus(updateReqVo);
        return CommonResult.success();
    }

    @Operation(summary = "审批主页箱量信息")
    @PostMapping("/getCntrData")
    @OperateLog(name = "审批主页箱量信息", type = OperateTypeEnum.GET)
    public CommonResult<CntrDataRespVO> getCntrData(@Valid @RequestBody SpecialCargoApplyPageReqVO pageVo) {
        CntrDataRespVO results = specialCargoApprovalService.getCntrData(pageVo);
        return CommonResult.success(results);
    }

}
